/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Library;
using System.IO;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Adds template to project item
    /// </summary>
    public class AddItemFromTemplateToProjectItemAction : ActionBase
    {
        private string _template = string.Empty;
        private EnvDTE.ProjectItem _inputProjectItem = null;
        private bool _open = false;
        private EnvDTE.ProjectItem _newItem = null;

        #region Constructors
        public AddItemFromTemplateToProjectItemAction()
            : base() 
        { 
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        public string Template
        {
            get
            {
                return this._template;
            }
            set
            {
                this._template = value;
            }
        }
        [Input(Required=true)]
        [CLSCompliant(false)]
        public EnvDTE.ProjectItem InputProjectItem
        {
            get
            {
                return this._inputProjectItem;
            }
            set
            {
                this._inputProjectItem = value;
            }
        }        
        [Input(Required=false)]
        public bool Open
        {
            get
            {
                return this._open;
            }
            set
            {
                this._open = value;
            }
        }
        [Output]
        [CLSCompliant(false)]
        public EnvDTE.ProjectItem NewItem
        {
            get
            {
                return this._newItem;
            }
        }        
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Get full path to file
            string fullFilePath = Path.Combine(new DirectoryInfo(this.PackageTemplatePath).FullName, 
                this._template);

            //Add file to project item
            this._newItem = this._inputProjectItem.ProjectItems.AddFromFileCopy(fullFilePath);
        }
        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._template)
                throw new ArgumentNullException("Template");
            if (0 == this._template.Length)
                throw new ArgumentOutOfRangeException("Template");
            if (null == this._inputProjectItem)
                throw new ArgumentNullException("InputProjectItem");
        }
        #endregion

        #region Private Methods
        #endregion
    }
}
